package com.flower.hooc.app.api.controller;

import com.flower.hooc.bean.pojo.entity.User;
import com.flower.hooc.bean.pojo.qo.UserLoginQO;
import com.flower.hooc.bean.pojo.vo.RespResult;
import com.flower.hooc.service.UserService;
import com.flower.hooc.util.constants.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:kevinfoo
 * @date:2020/08/27
 * @file:com.flower.controller
 */
@CrossOrigin
@RestController
@RequestMapping("/hooc/user")
@Api(tags = "用户业务接口")
public class UserController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @GetMapping("/info")
    @ApiOperation("根据用户ID获取用户信息")
    @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID"
            , required = true, dataType = "long")
    public RespResult info(Long userId) {
        logger.info("info userId:{}", userId);
        User user = userService.getById(userId);
        return returnRespWithSuccess(user);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public RespResult login(@RequestBody UserLoginQO qo, HttpServletRequest request) {
        logger.info("login qo:{}", qo);
        System.out.println("userLoginqo is : " + qo);
        //验证码判断
        String verifyCodeActual = qo.getVCode();
        String verifyCodeExpected = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
        System.out.println("verifyCodeExpected is : " + verifyCodeExpected);
        System.out.println("verifyCodeActual is : " + verifyCodeActual);
        //todo 验证码的servelt没移植过来
        /*if (verifyCodeActual == null) {
            //验证码错误
            System.out.println("验证码错误");
            return returnRespWithFailed("验证码错误");
        }*/

        RespResult result = new RespResult();
        User dbUser = userService.login(qo);
        if (dbUser == null) {
            //用户不存在
            System.out.println("用户不存在");
            return returnRespWithFailed("用户不存在");
        }
        //密码是否正确
        if (!dbUser.getPassword().equals(qo.getPassword())) {
            //密码错误
            System.out.println("密码错误");
            return returnRespWithFailed("密码错误");
        }
        //登录成功，用户信息存到session
        request.getSession().setAttribute(Constants.SESSION_KEY_LOGIN_USER, dbUser);
        System.out.println("in usercontroller user is : " + dbUser);
        return returnRespWithSuccess(dbUser);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public RespResult register(@RequestBody User user, HttpServletRequest request) {
        logger.info("login user{}", user);
        User dbUser = userService.register(user);
        System.out.println("dbuser" + dbUser);
        //登录成功，用户信息存到session
        request.getSession().setAttribute(Constants.SESSION_KEY_LOGIN_USER, dbUser);

        return returnRespWithSuccess(dbUser);
    }

    @GetMapping("/showUserByName")
    @ApiOperation("根据用户名获取用户信息")
    @ApiImplicitParam(paramType = "query", name = "username", value = "用户名"
            , required = true, dataType = "String")
    public RespResult getByUserName(String userName) {
        logger.info("showUserByName userame:{}", userName);
        User user = userService.getByUserName(userName);
        return returnRespWithSuccess(user);
    }

}
